﻿namespace 自定义中间件
{
    //.net框架中约定，会把后缀为Middleware的类识别为自定义的中间件，可以加入中间件处理
    public class CustomMiddleware
    {
        private readonly RequestDelegate _delegate;

        public CustomMiddleware(RequestDelegate @delegate)
        {
            _delegate = @delegate;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            Console.WriteLine("在请求处理之前执行：todo");
            await _delegate(context);
            Console.WriteLine("在请求处理之后执行：todo");
        }
    }

    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder app)
        {
            return app.UseMiddleware<CustomMiddleware>();
        }
    }
}